﻿Imports System.Data.SqlClient

Public Class AddProfile

    Private Sub txtcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        Me.Close()
    End Sub

    Private Sub txtadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        If txtname.Text.Trim <> "" Then
            If ExistProfile(txtname.Text.Trim) = False Then
                Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
                Dim cmd As New SqlCommand("INSERT into perfiles(nombre) values (@nombre)", cnn)
                cmd.Parameters.AddWithValue("@nombre", txtname.Text.Trim)
                Using cnn
                    cnn.Open()
                    cmd.ExecuteNonQuery()
                    cnn.Close()
                End Using
                Config.FillComboBox(Config.cbxprofiles, "Select * from Perfiles order by nombre", 1)
                EditProfile.Actualizar()
                MsgBox("The profile " + txtname.Text.Trim + " has been created succesfully!", MsgBoxStyle.OkOnly, "Profile created!!")
                txtname.Text = ""
            Else
                MsgBox("The profile name '" + txtname.Text.Trim + "' already exist!", MsgBoxStyle.Critical, "Profile alert!")
            End If
        Else
            MsgBox("The name field can't be empty!", MsgBoxStyle.OkOnly, "Profile alert!")
        End If
        txtname.Focus()
    End Sub

    Public Function ExistProfile(ByVal name As String) As Boolean
        Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
        Dim cmd As New SqlCommand("select count(*) from perfiles where (nombre = @nombre)", cnn)
        Dim result As Integer
        cmd.Parameters.AddWithValue("@nombre", name)
        Using cnn
            cnn.Open()
            result = cmd.ExecuteScalar()
            cnn.Close()
            If result > 0 Then
                Return True
            Else
                Return False
            End If
        End Using
    End Function

End Class

